package com.freesky.provider.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.freesky.common.dao.entity.Item;
import com.freesky.common.enums.RedisKeyEnum;
import com.freesky.common.redis.IRedisTemplate;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.common.vo.CartItem;
import com.freesky.provider.order.service.ICartService;
import com.freesky.provider.order.util.VOUtil;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Freesy
 */
@Service
public class CartServiceImpl implements ICartService {


    @Autowired
    private IRedisTemplate redisTemplate;

    @Override
    public JSONResponse addCart(long userId, String itemId, int num) {

        //hash: "key:用户id" field："商品id" value："商品信息"
        Boolean hexists = redisTemplate.hexists(RedisKeyEnum.MEMBER_CART_PREFIX.getKey() + ":" + userId, itemId + "");
        //如果存在数量相加
        if (hexists) {
            CartItem cartItem = redisTemplate.hget(RedisKeyEnum.MEMBER_CART_PREFIX.getKey() + ":" + userId, itemId + "", CartItem.class);
            if (cartItem != null) {
                cartItem.setProductNum(cartItem.getProductNum() + num);
                redisTemplate.hset(RedisKeyEnum.MEMBER_CART_PREFIX.getKey() + ":" + userId, itemId + "", cartItem);
            } else {
                return JSONResponseDiretor.buildErrorJSONResponse(null);
            }

            return JSONResponseDiretor.buildSuccessJSONResponse(null);
        }
        //如果不存在，根据商品id取商品信息
        Item item= new Item();
        item.setId(itemId);
        item = item.selectById();
        if (item == null) {
            return JSONResponseDiretor.buildErrorJSONResponse(null);
        }
        CartItem cartItem = VOUtil.changeItemToCartItem(item);
        cartItem.setProductNum((long) num);
        cartItem.setChecked("1");
        redisTemplate.hset(RedisKeyEnum.MEMBER_CART_PREFIX.getKey() + ":" + userId, itemId + "", JSON.toJSON(cartItem));
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public JSONResponse getCartList(long userId) {
        List<CartItem> list = redisTemplate.hvals(RedisKeyEnum.MEMBER_CART_PREFIX.getKey() + ":" + userId, CartItem.class);
        return JSONResponseDiretor.buildSuccessJSONResponse(list);
    }

    @Override
    public JSONResponse updateCartNum(long userId, String itemId, int num, String checked) {
        String json = redisTemplate.hget(RedisKeyEnum.MEMBER_CART_PREFIX.getKey() + ":" + userId, itemId + "", String.class);
        if (json == null) {
            return JSONResponseDiretor.buildErrorJSONResponse(null);
        }
        CartItem cartItem = new Gson().fromJson(json, CartItem.class);
        cartItem.setProductNum((long) num);
        cartItem.setChecked(checked);
        redisTemplate.hset(RedisKeyEnum.MEMBER_CART_PREFIX.getKey() + ":" + userId, itemId + "", JSON.toJSON(cartItem));
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public JSONResponse checkAll(long userId, String checked) {
        List<String> jsonList = redisTemplate.getCacheList(RedisKeyEnum.MEMBER_CART_PREFIX.getKey() + ":" + userId, String.class);

        for (String json : jsonList) {
            CartItem cartItem = new Gson().fromJson(json, CartItem.class);
            if ("true".equals(checked)) {
                cartItem.setChecked("1");
            } else if ("false".equals(checked)) {
                cartItem.setChecked("0");
            } else {
                return JSONResponseDiretor.buildErrorJSONResponse(null);
            }
            redisTemplate.hset(RedisKeyEnum.MEMBER_CART_PREFIX.getKey() + ":" + userId, cartItem.getProductId() + "", JSON.toJSON(cartItem));
        }

        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public JSONResponse deleteCartItem(long userId, String itemId) {
        redisTemplate.hdel(RedisKeyEnum.MEMBER_CART_PREFIX.getKey() + ":" + userId, itemId + "");
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

    @Override
    public JSONResponse delChecked(long userId) {
        List<String> jsonList = redisTemplate.getCacheList(RedisKeyEnum.MEMBER_CART_PREFIX.getKey() + ":" + userId, String.class);
        for (String json : jsonList) {
            CartItem cartItem = new Gson().fromJson(json, CartItem.class);
            if ("1".equals(cartItem.getChecked())) {
                redisTemplate.hdel(RedisKeyEnum.MEMBER_CART_PREFIX.getKey() + ":" + userId, cartItem.getProductId() + "");
            }
        }
        return JSONResponseDiretor.buildSuccessJSONResponse(null);
    }

}
